package com.tokencloud.consumer.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;


@Component
public class SpringRabbitListener {

    private static final Logger log = LoggerFactory.getLogger(SpringRabbitListener.class);
    private final RabbitTemplate rabbitTemplate;

    public SpringRabbitListener(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "simple.queue"),
            exchange = @Exchange(value = "LYC.direct", type = ExchangeTypes.DIRECT),
            key = "simple"
    ))
    void listenSimpleQueue(Map message) {
        System.out.println("接收到消息：{}" + message);

//        throw new MessageConversionException("故意的");
    }

    /**
     * Work模型的使用：
     *  1、多个消费者绑定到一个队列，可以加快消息的处理。
     *  2、同一条消息只会被同一个消费者处理。
     *  3、通过设置prefetch 来控制消费者预取的消息数量，处理完一条再处理下一条，实现能者多劳。
     *
     */

//    @RabbitListener(queues = "work.queue")
//    void listenWorkQueue1(String message) throws InterruptedException {
//        System.out.println("work.queue1 接收到消息：{}" + message);
//        // 机器性能高，需耗时20 mills
//        Thread.sleep(20);
//    }
//
//    @RabbitListener(queues = "work.queue")
//    void listenWorkQueue2(String message) throws InterruptedException {
//        System.out.println("work.queue2 >>>>>>>> 接收到消息：{}" + message);
//        // 机器性能低，需耗时200 mills
//        Thread.sleep(200);
//    }
//
//    /**
//     * 新建一个名为 LYC.fanout的交换机
//     * 分别新建名为 fanout.queue1和 fanout.queue2的队列
//     * 将 fanout.queue1和 fanout.queue2绑定至 LYC.fanout
//
//     * 交换机的作用
//     *  1、接收 publisher 发送的消息
//     *  2、将消息按照规则路由到与之绑定的队列
//     *  3、FanoutExchange会将消息路由到每个绑定的队列
//     */
//
//    @RabbitListener(queues = "fanout.queue1")
//    void listenFanoutQueue1(String message) throws InterruptedException {
//        System.out.println("fanout.queue1 接收到消息：{}" + message);
//        // 机器性能高，需耗时20 mills
//        Thread.sleep(20);
//    }
//
//    @RabbitListener(queues = "fanout.queue2")
//    void listenFanoutQueue2(String message) throws InterruptedException {
//        System.out.println("fanout.queue2 >>>>>>>> 接收到消息：{}" + message);
//        // 机器性能低，需耗时200 mills
//        Thread.sleep(200);
//    }
//
//    @RabbitListener(queues = "direct.queue1")
//    void listenDirectQueue1(String message) throws InterruptedException {
//        System.out.println("direct.queue1 接收到消息：{}" + message);
//        // 机器性能高，需耗时20 mills
//        Thread.sleep(20);
//    }
//
//    @RabbitListener(queues = "direct.queue2")
//    void listenDirectQueue2(String message) throws InterruptedException {
//        System.out.println("direct.queue2 >>>>>>>> 接收到消息：{}" + message);
//        // 机器性能低，需耗时200 mills
//        Thread.sleep(200);
//    }
//
//    @RabbitListener(queues = "topic.queue1")
//    void listenTopicQueue1(String message) throws InterruptedException {
//        System.out.println("topic.queue1 接收到消息：{}" + message);
//        // 机器性能高，需耗时20 mills
//        Thread.sleep(20);
//    }
//
//    @RabbitListener(queues = "topic.queue2")
//    void listenTopicQueue2(String message) throws InterruptedException {
//        System.out.println("topic.queue2 >>>>>>>> 接收到消息：{}" + message);
//        // 机器性能低，需耗时200 mills
//        Thread.sleep(200);
//    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "fanout.queue3"),
            exchange = @Exchange(value = "LYCFanout2", type = ExchangeTypes.FANOUT)
    ))
    void listenFanoutQueue3(String message) {

    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "fanout.queue4"),
            exchange = @Exchange(value = "LYCFanout2", type = ExchangeTypes.FANOUT)
    ))
    void listenFanoutQueue4(String message) {

    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue6"),
            exchange = @Exchange(value = "LYC.direct2", type = ExchangeTypes.DIRECT),
            key = "red, blue"
    ))
    void listenDirectQueue6(String message) {


    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue",durable = "true"),
            exchange = @Exchange(name = "delay.direct", delayed = "true"),
            key = "delay"
        ))
    public void listenDelayMessage(String message) {
        log.error("接收到 延时消息：{}", message);

    }
}
